<?php

namespace App\Filament\Resources;

use App\Filament\Resources\AuthorResource\Pages;
use App\Filament\Resources\AuthorResource\RelationManagers;
use App\Models\Author;
use Carbon\Carbon;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;

class AuthorResource extends Resource
{
    protected static ?string $model = Author::class;

    protected static ?string $navigationIcon = 'heroicon-o-user-circle';

    protected static ?int $navigationSort = 3;

    // 导航项分组翻译
    public static function getNavigationGroup(): ?string
    {
        return __('menu.article_group.name');
    }

    // 菜单标题导航翻译
    public static function getNavigationLabel(): string
    {
        return __('menu.article_group.author');
    }

    // 面包屑导航翻译
    public static function getModelLabel(): string
    {
        return __('menu.article_group.author');
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                FileUpload::make('avatar')
                    ->label(__('user.Avatar'))
                    ->avatar()
                    ->getUploadedFileNameForStorageUsing(
                        function (TemporaryUploadedFile $file) {
                            return (string)str('avatar/' . date('Ymd') . '/' . $file->getClientOriginalName());
                        }
                    ),
                TextInput::make('name')->label(__('author.Name'))->required(),
                Select::make('gender')
                    ->label(__('author.Gender'))
                    ->options([
                        '未知' => '未知',
                        '男'   => '男',
                        '女'   => '女',
                    ])
                    ->required(),
                DatePicker::make('birthday')
                    ->label(__('author.Birthday'))
                    ->required(),
                Textarea::make('information')
                    ->label(__('author.Information'))
                    ->rows(5),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('avatar')
                    ->label(__('author.Avatar'))
                    ->circular()
                    ->size(80)
                    ->alignCenter(),
                TextColumn::make('name')
                    ->label(__('author.Name'))
                    ->badge()
                    ->alignCenter(),
                TextColumn::make('gender')->label(__('author.Gender'))->alignCenter(),
                TextColumn::make('birthday')
                    ->label(__('author.Birthday'))
                    ->description(function (Author $record) {
                        return Carbon::parse($record['birthday'])->age . '岁';
                    })
                    ->alignCenter(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListAuthors::route('/'),
        ];
    }
}
